package com.gjun.struts.action;

import java.util.Date;
import java.util.List;
import java.util.Properties;
import java.util.UUID;

import javax.mail.Address;
import javax.mail.Message;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

import org.apache.openjpa.lib.util.UUIDGenerator;
import org.apache.struts2.ServletActionContext;
import org.hibernate.id.UUIDHexGenerator;

import com.gjun.struts.bean.GjunRoleJsonBean;
import com.gjun.struts.bean.GjunsystemRole;
import com.gjun.struts.bean.GjunsystemUser;
import com.gjun.struts.resource.GjunRoleResourceIf;
import com.gjun.struts.resource.GjunSystemUserResourceIf;
import com.gjun.struts.util.EmailSenderHelper;
import com.gjun.struts.util.JSONHelper;
import com.gjun.struts.util.MD5;

public class SaveSystemUserAction {
    private String result;
    
    private String username;
    
    private String passwd;
    
    private String email;
    
    private String qq;
    
    private String tel;
    
    private String[] roles;
    
    
    
    private GjunRoleResourceIf roleResourceDAO;
    
    
    private GjunSystemUserResourceIf userResourceDAO;
    
    
    
    
    

	public GjunSystemUserResourceIf getUserResourceDAO() {
		return userResourceDAO;
	}

	public void setUserResourceDAO(GjunSystemUserResourceIf userResourceDAO) {
		this.userResourceDAO = userResourceDAO;
	}

	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	public String getPasswd() {
		return passwd;
	}

	public void setPasswd(String passwd) {
		this.passwd = passwd;
	}

	public String getQq() {
		return qq;
	}

	public void setQq(String qq) {
		this.qq = qq;
	}

	public String[] getRoles() {
		return roles;
	}

	public void setRoles(String[] roles) {
		this.roles = roles;
	}

	public String getTel() {
		return tel;
	}

	public void setTel(String tel) {
		this.tel = tel;
	}

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public GjunRoleResourceIf getRoleResourceDAO() {
		return roleResourceDAO;
	}

	public void setRoleResourceDAO(GjunRoleResourceIf roleResourceDAO) {
		this.roleResourceDAO = roleResourceDAO;
	}

	public String getResult() {
		return result;
	}

	public void setResult(String result) {
		this.result = result;
	}
	
	
	public String saveSystemUser(){
		
		boolean isHaveName = userResourceDAO.loadUserExistByName(username);
		if(isHaveName){
			result = "haveUserName";
			return "success";
		}
		
		boolean isHaveEmail = userResourceDAO.loadExistByEmail(email);
		if(isHaveEmail){
			result = "haveEmail";
			return "success";
		}
		
		GjunsystemUser user = new GjunsystemUser();
		user.setEmail(email);
		user.setPasswd(MD5.toMD5(passwd));
		user.setQq(qq);
		user.setRegdate(new Date());
		user.setState(0);
		user.setTel(tel);
		user.setUsername(username);
		user.setCode(new com.gjun.struts.util.UUIDGenerator().generate().toString());
		
		try {
			userResourceDAO.saveSystemUser(user, roles);
			result = "saveSuccess";
			
			// 下面完成邮件的发送
			String path = "http://localhost:8080"+ServletActionContext.getRequest().getContextPath()+"/user/stateChangeAction.action?username="+user.getUsername()+"&code="+user.getCode();
			//System.out.println(path);
			boolean isSend = EmailSenderHelper.sendEmailToSystemUser(user,path);
			System.out.println("user passwd:  "+user.getPasswd());
			if(isSend){
				result += "+sendSuccess";
			}else{
				result = "+sendFaild";
			}
			
	 		
			
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			result = "saveFaild";
		}
		System.out.println(result);
		return "success";
	}
    
    public String loadRoles(){
    	
    	
    	try {
    		List<GjunRoleJsonBean> roles = roleResourceDAO.loadAllRoles();
        	
			result = JSONHelper.roleToJson(roles);
			//System.out.println("result:  "+result);
		} catch (Throwable e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
    	return "success";
    }
    
    public static void main(String[] args) {
		System.out.println(UUIDGenerator.nextHex());
	}
}
